﻿using System;
using System.Collections.Generic;
using NHibernate;
using SimpleScrum.Core.Entities;
using SimpleScrum.Helpers;
using SimpleScrum.Utils;

namespace SimpleScrum.Areas.Scrum.Models
{
    public class UserStoryModel
    {
        private readonly Project _project = new ProjectModel().GetProjectById(Client.Instance.CurrentProjectId);
        private ISession _session;

        public IList<UserStory> GetUserStorys(Guid projectId)
        {
            IList<UserStory> userStorys;
            using (_session = NHibernateHelper.GetSession())
            {
                //NHibernate query 
                IQuery query =
                    _session.CreateQuery("from UserStory where ProjectId='" + projectId + "';");
                userStorys = query.List<UserStory>();
            }
            return userStorys;
        }

        public UserStory GetUserStoryById(Guid id)
        {
            var userStory = new UserStory();
            using (_session = NHibernateHelper.GetSession())
            {
                using (ITransaction tran = _session.BeginTransaction())
                {
                    userStory = _session.Get<UserStory>(id);
                    tran.Commit();
                }
            }
            return userStory;
        }

        public bool NewUserStory(UserStory userStory)
        {
            using (_session = NHibernateHelper.GetSession())
            {
                using (ITransaction tran = _session.BeginTransaction())
                {
                    //userStory.ProjectId = _currentProject;
                    userStory.AssignDate = DateTime.Now;
                    userStory.CreatedDate = DateTime.Now;
                    userStory.LastUpdate = DateTime.Now;
                    userStory.CreatedDate = DateTime.Now;
                    userStory.Project = _project;
                    userStory.CreatedBy = Client.Instance.ClientId;

                    _session.Save(userStory);
                    tran.Commit();
                }
            }
            return true;
        }

        public void UpdateUserStory(UserStory userStory)
        {
            using (_session = NHibernateHelper.GetSession())
            {
                using (ITransaction tran = _session.BeginTransaction())
                {
                    //userStory.ProjectId = _currentUser;
                    userStory.LastUpdate = DateTime.Now;
                    _session.Update(userStory);
                    tran.Commit();
                }
            }
        }

        public void DeleteUserStory(UserStory userStory)
        {
            using (_session = NHibernateHelper.GetSession())
            {
                using (ITransaction tran = _session.BeginTransaction())
                {
                    try
                    {
                        _session.Delete(userStory);
                        tran.Commit();
                    }
                    catch (Exception ex)
                    {
                        string msg = ex.Message;
                    }
                }
            }
        }
    }
}